<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/


if (!is_admin()) exit;

if (isset ($_POST['submit'])) {
	$name = $_POST['iname'];
	$link = $_POST['ilink'];
	$group = $_POST['igroup'];
	
	
	if ($name == '' || $link == '') { alert ('Не задано имя или ссылка!'); exit; }
	
	
	
	if ($db->query ("INSERT INTO ".DB_PEREFIX."_admin_cp VALUES (0, '$group', '$link', '$name', 'images/admin/default.gif')")) {
		
		if ($_FILES['image']['name'] != '') {
			if (eregi('[A-Za-z0-9_]\.(jpeg|jpg|gif|png|bmp|JPEG|JPG|GIF|PNG|BMP)', $_FILES['image']['name'])) {
				if (copy($_FILES['image']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/include/cache/'.$_FILES['image']['name'])) {
					
					try {
						
						include $_SERVER['DOCUMENT_ROOT'].'/engine/image.class.php';
						
						$image = new imageResize (
								$_SERVER['DOCUMENT_ROOT'].'/include/cache/'.$_FILES['image']['name'],
								mysql_insert_id(),
								'images/admin',
								array(100, 100)
							);
						$image->resize ();
						
						$db->query ("UPDATE ".DB_PEREFIX."_admin_cp SET image = 'images/admin/".mysql_insert_id().".jpg' WHERE id = ".mysql_insert_id ());
					}
					catch (Exception $e) {
						alert ($e->getMessage());
						$db->query ("DELETE FROM ".DB_PEREFIX."_admin_cp WHERE id = ".mysql_insert_id ());
						exit;
					}
					
				}
				else {
					alert ('Ошибка при добавлении изоображения!');
					$db->query ("DELETE FROM ".DB_PEREFIX."_admin_cp WHERE id = ".mysql_insert_id ());
					exit;
				}
			}
			else {
				alert ('Файл не является изоображением!');
				$db->query ("DELETE FROM ".DB_PEREFIX."_admin_cp WHERE id = ".mysql_insert_id ());
				exit;
			}
		}
		
		print "<script type=\"text/javascript\">parent.reload_buttons();</script>";
		exit ();
		
		
	}
	else alert ('Ошибка при добавлении кнопки!');
}

function alert ($text) {
	print "<script type=\"text/javascript\">alert (\"$text\");</script>";
}